package com.penton.util.http;

import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;

import android.content.Context;
/**
	 // Instantiate the custom HttpClient
	DefaultHttpClient client = new MyHttpClient(getApplicationContext());
	HttpGet get = new HttpGet("https://www.mydomain.ch/rest/contacts/23");
	// Execute the GET call and obtain the response
	HttpResponse getResponse = client.execute(get);
	HttpEntity responseEntity = getResponse.getEntity();
 * @author pengjianbo <pengjianbosoft@gmail.com>
 *http://blog.antoine.li/2010/10/22/android-trusting-ssl-certificates/
 *http://stackoverflow.com/questions/995514/https-connection-android
 *http://www.cnblogs.com/hll2008/archive/2011/04/30/2033481.html
 *
 *可以请求Https://
 */
public class MyHttpClient extends DefaultHttpClient {
	final Context context;
	 
    public MyHttpClient(Context context) {
        this.context = context;
    }
 
    @Override
    protected ClientConnectionManager createClientConnectionManager() {
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        // Register for port 443 our SSLSocketFactory with our keystore
        // to the ConnectionManager
        try {
    	 	KeyStore keystore = KeyStore.getInstance(/*"BKS"*/KeyStore.getDefaultType());
		registry.register(new Scheme("https", new MySSLSocketFactory(keystore), 443));
		} catch (KeyManagementException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (KeyStoreException e) {
			e.printStackTrace();
		} catch (UnrecoverableKeyException e) {
			e.printStackTrace();
		}
        return new SingleClientConnManager(getParams(), registry);
    }
 
  /*  private SSLSocketFactory newSslSocketFactory() {
        try {
            // Get an instance of the Bouncy Castle KeyStore format
            KeyStore trusted = KeyStore.getInstance("BKS");
            // Get the raw resource, which contains the keystore with
            // your trusted certificates (root and any intermediate certs)
            InputStream in = context.getResources().openRawResource(R.raw.mykeystore);
            try {
                // Initialize the keystore with the provided trusted certificates
                // Also provide the password of the keystore
                trusted.load(in, "mysecret".toCharArray());
            } finally {
                in.close();
            }
            // Pass the keystore to the SSLSocketFactory. The factory is responsible
            // for the verification of the server certificate.
            SSLSocketFactory sf = new SSLSocketFactory(trusted);
            // Hostname verification from certificate
            // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
            sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
            return sf;
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }*/
}
